#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Mikan RSS View Module

Mikan Python Web Framework
"""

__author__ = "K.Sonohara"
__status__ = "develop"
__version__ = "0.0.0_7"
__date__    = "2014/05/05"
__license__ = 'LGPL'
# ------------------------------------------------------------
## Import Section
# ------------------------------------------------------------
# Python

# feedparser
import feedparser

# ESiPC
from esipc import ESiPCProperty

# Mikan
from mikan.view import MikanJSONView

# ------------------------------------------------------------
## Variable Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Mikan Contant
# ------------------------------------------------------------

# ------------------------------------------------------------
## Function Section
# ------------------------------------------------------------

# ------------------------------------------------------------
## Class Section
# ------------------------------------------------------------

class MikanRSS2JSONView(MikanJSONView):

	def set_count(self, value):
		self.FCount = value

	def get_count(self):
		return self.FCount

	def get_items(self):
		return self.FItems

	def __init__(self, request, items=None, count=20):
		super(MikanRSS2JSONView, self).__init__(request)

		self.FItems = items
		if items == None:
			self.FItems = []
		self.FCount = count

	def perform(self):
		jsons = []
		items = []

		for url in self.items:
			try:
				rss = feedparser.parse(url)

				if rss:
					rtitle = ""
					try:
						rtitle = rss.feed.title
					except:
						pass

					count = 0
					for item in rss.entries:
						if count < 20:
							d =  u"%d年%02d月%02d日 %02d時 %02d分" % (item.updated_parsed.tm_year, item.updated_parsed.tm_mon, item.updated_parsed.tm_mday, item.updated_parsed.tm_hour, item.updated_parsed.tm_min)
							title = item.title
							link = item.link

							item = {}
							item["rtitle"] = rtitle
							item["title"] = title
							item["date"] = d
							item["link"] = link

							items.append(item)
						else:
							break
						count = count + 1
			except:
				self.logger.error(traceback.format_exc())

		items.sort(key=lambda x:(x["date"],x["rtitle"]), reverse=True)

		count = 0
		for item in items:
			if (self.count != -1) and (count >= self.count):
				break

			jsons.append({
				"url": item["link"],
				"date": item["date"],
				"type": item["rtitle"],
				"title": item["title"],
			})
			count += 1

		self.result = { "items": jsons }

		return super(MikanRSS2JSONView, self).perform()

	# ----------------------------------------------------
	## Property
	count = ESiPCProperty(get_count, set_count, None)
	items = ESiPCProperty(get_items, None, None)

# ------------------------------------------------------------
## Main Section
# ------------------------------------------------------------

# ------------------------------------------------------------
